/**
 * Created by foxit on 15-10-20.
 */
define(function (require, exports, module) {  
    var CMISService = require("./CMISService");   
    var FolderTree = require("./component/FolderTree");
    var SearchTable = require("./component/SearchTable");
    $(function(){
        var currentPage = 1;
        
        CMISService.getFolderTree().done(function(data) {
            var folder_action = null;
            ReactDOM.render(React.createElement(FolderTree, {
                    data: data,
                    option: {
                        id: 'folderNav',
                        style: 'tree-solid-line'
                    }
            }, folder_action), $("#document-navigation .tree-box")[0]);

            $('#folderNav').on('clickNode', function(e, node) {     
            });
        });
    
        showResult(searchResult);

        $('#search-btn').click(function() {
            var keyword = $("#search-input").val();           
            CMISService.doSearch(keyword, {currentPage: 1}).done(function(searchResult) {               
                showResult(searchResult);
            });
        });

       

        function showResult(searchResult) {
            React.render(React.createElement(SearchTable, {
                data: searchResult.data
            }), $("#searchTable")[0]);
            $("#search-status").html("本次搜索找到{0}个匹配的文档".format(searchResult.numItems));
            var options = {
                currentPage: searchResult.currentPage,
                totalPages: searchResult.totalPages,
                bootstrapMajorVersion:3,
                alignment: 'right',
                onPageClicked: function(e,originalEvent,type, page){                  
                    var keyword = $("#search-input").val();  
                    CMISService.doSearch(keyword, {currentPage: page}).done(function(searchResult) {                  
                       showResult(searchResult);
                    });
                }
            };
            $('#pager').bootstrapPaginator(options);
        }

        $(".change_icon").click(function(){
            if($('.list_nav').is(":visible") === true)
            {
                $(this).find('.up-down').attr("class","fa fa-chevron-down up-down");
            }
            else
            {
                $(this).find('.up-down').attr("class","fa fa-chevron-up up-down");
            }
        });
    });
});
